package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;

import reader.ScenarioBuilder;
import action.ActionPool;
import constants.Constants;

public class PanelModel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel ScenarioPanel = null;
	private JScrollPane ScenarioScroll = null;
	private JTextArea ScenarioTextArea = null;
	private JLabel StatusLabel = null;
	private JButton SaveScenario;
	private JButton EditScenario;
	private JButton LoadScenario;
	private JButton Compile = null;
	private JButton Generate = null;
	private int index;

	/**
	 * This is the default constructor
	 */
	public PanelModel(int index) {
		super();
		initialize();
		this.index = index;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(540, 360);
		StatusLabel = new JLabel();
		StatusLabel.setBounds(new Rectangle(420, 10, 120, 35));
		StatusLabel.setText("");
		this.setLayout(null);
		this.setBackground(Color.white);
		this.add(getScenarioPanel(), null);
		this.add(StatusLabel, null);
		this.add(getLoadScenario(), null);
		this.add(getEditScenario(), null);
		this.add(getSaveScenario(), null);
		this.add(getCompile(), null);
		this.add(getGenerate(), null);
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */

	private JButton getLoadScenario() {
		if (LoadScenario == null) {
			LoadScenario = new JButton();
			LoadScenario.setForeground(new Color(0, 0, 102));
			LoadScenario.setBounds(new Rectangle(420, 85, 90, 25));
			LoadScenario.setText("Load");
			LoadScenario.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showOpenDialog(LoadScenario);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = fc.getSelectedFile();
						String res = read(file);
						ScenarioTextArea.setText(res);
						Compile.setEnabled(true);
					}
				}
			});
		}
		return LoadScenario;
	}

	private String read(File f) {
		String result = new String(), nextLine = new String();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(f));
			while ((nextLine = reader.readLine()) != null)
				result = result.concat(nextLine).concat("\n");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	private JButton getEditScenario() {
		if (EditScenario == null) {
			EditScenario = new JButton();
			EditScenario.setForeground(new Color(0, 0, 102));
			EditScenario.setBounds(new Rectangle(420, 120, 90, 25));
			EditScenario.setText("Edit");
			EditScenario.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ScenarioTextArea.setEditable(true);
					Compile.setEnabled(true);
				}
			});
		}
		return EditScenario;
	}

	private JButton getSaveScenario() {
		if (SaveScenario == null) {
			SaveScenario = new JButton();
			SaveScenario.setForeground(new Color(0, 0, 102));
			SaveScenario.setBounds(new Rectangle(420, 155, 90, 25));
			SaveScenario.setText("Save");
			SaveScenario.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int returnVal = fc.showSaveDialog(SaveScenario);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						File file = fc.getSelectedFile();
						saveScenario(file);
					}
				}
			});
		}
		return SaveScenario;
	}

	protected void saveScenario(File file) {
		PrintWriter pw;
		try {
			pw = new PrintWriter(file);
			pw.write(ScenarioTextArea.getText());
			pw.flush();
			pw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * This method initializes ScenarioPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getScenarioPanel() {
		if (ScenarioPanel == null) {
			ScenarioPanel = new JPanel();
			ScenarioPanel.setLayout(null);
			ScenarioPanel.setBounds(new Rectangle(10, 10, 400, 340));
			ScenarioPanel.setBackground(Color.white);
			ScenarioPanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory.createTitledBorder(BorderFactory
							.createLineBorder(Color.gray, 5), "Scenario",
							TitledBorder.DEFAULT_JUSTIFICATION,
							TitledBorder.DEFAULT_POSITION, new Font(
									"SansSerif", Font.BOLD, 14),
							SystemColor.textHighlight)));
			ScenarioPanel.add(getScenarioScroll(), null);
		}
		return ScenarioPanel;
	}

	/**
	 * This method initializes ScenarioScroll
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getScenarioScroll() {
		if (ScenarioScroll == null) {
			ScenarioScroll = new JScrollPane();
			ScenarioScroll.setBounds(new Rectangle(12, 22, 375, 305));
			ScenarioScroll.setViewportView(getScenarioTextArea());
		}
		return ScenarioScroll;
	}

	/**
	 * This method initializes ScenarioTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getScenarioTextArea() {
		if (ScenarioTextArea == null) {
			ScenarioTextArea = new JTextArea();
			ScenarioTextArea.setEditable(false);
			ScenarioTextArea.setLocation(new Point(0, 0));
			ScenarioTextArea.setFont(new Font("SansSerif", Font.BOLD, 12));
		}
		return ScenarioTextArea;
	}

	public void setStatus(Color c, String msg) {
		StatusLabel.setText(msg);
		StatusLabel.setForeground(c);
	}

	/**
	 * This method initializes Compile
	 * 
	 * @return javax.swing.JButton
	 */

	public boolean isCompiled = false;

	private JButton getCompile() {
		if (Compile == null) {
			Compile = new JButton();
			Compile.setForeground(new Color(0, 0, 102));
			Compile.setBounds(new Rectangle(420, 50, 110, 25));
			Compile.setText("Compile");
			Compile.setEnabled(false);
			Compile.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ScenarioBuilder sb = new ScenarioBuilder();
					ActionPool.clear();
					boolean res = sb.build(ScenarioTextArea.getText());
					if (res == true) {
						setStatus(Constants.green,
								"<html>Compilation<br />succesful</html>");
						Compile.setEnabled(false);
						ScenarioTextArea.setEditable(false);
						if (!isCompiled) {
							ScenarioDialog.compiled[index] = true;
							isCompiled = true;
							if (ScenarioDialog.allCompiled()) {
								MainGUI.ActivateSV(true);
							}
						}
					} else {
						setStatus(Constants.red,
								"<html>Compilation<br />failed</html>");
						if (isCompiled == true) {
							ScenarioDialog.compiled[index] = false;
							MainGUI.ActivateSV(false);
							isCompiled = false;
						}
					}

				}
			});
		}
		return Compile;
	}

	/**
	 * This method initializes Generate
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getGenerate() {
		if (Generate == null) {
			Generate = new JButton();
			Generate.setBounds(new Rectangle(420, 190, 90, 25));
			Generate.setForeground(new Color(0, 0, 102));
			Generate.setText("Generate");
		}
		return Generate;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
